package com.aaa.vegetablemarketexpress.service.impl;

import com.aaa.vegetablemarketexpress.dao.VarietyDao;
import com.aaa.vegetablemarketexpress.entity.User;
import com.aaa.vegetablemarketexpress.entity.Variety;
import com.aaa.vegetablemarketexpress.service.VarietyService;
import com.aaa.vegetablemarketexpress.util.CustomException;
import com.aaa.vegetablemarketexpress.util.Page;
import com.aaa.vegetablemarketexpress.util.ResultStatus;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.catalina.security.SecurityUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

/**
 * @FileName: VarietyServiceImpl
 * @Description:
 * @Author: qxh
 * @CreateTime: 2025/9/8 11:42
 * @Version: 1.0.0
 */
@Service
public class VarietyServiceImpl implements VarietyService {

    @Resource
    private HttpSession session;
    @Resource
    private VarietyDao varietyDao;
    /**
     * 根据VarietyId查询商品品类
     * @param varietyId
     * @return
     */
    @Override
    public Variety queryByVarietyId(Long varietyId) {
        return varietyDao.queryByVarietyId(varietyId);
    }
    /**
     * 根据参数查询商品品类
     * @param page
     * @return
     */
    @Override
    public PageInfo queryByParam(Page<Variety> page) {
        Integer pageNo = page.getPageNo();
        Integer pageSize = page.getPageSize();
        if (pageNo == null || pageSize == null||pageNo == 0||pageSize == 0) {
            throw new CustomException(ResultStatus.PARAM_ISNULL.getStatusCode(), ResultStatus.PARAM_ISNULL.getStatusMsg());
        }
        PageHelper.startPage(pageNo,pageSize);
        Variety data = page.getData();
        List<Variety> list = varietyDao.queryByParam(data);
        return new PageInfo<>(list);
    }
    /**
     * 根据VarietyName查询商品品类
     * @param varietyName
     * @return
     */
    @Override
    public Variety queryByVarietyName(String varietyName) {
        return varietyDao.queryByVarietyName(varietyName);
    }
    /**
     * 添加商品品类
     * @param variety
     * @return
     */
    @Override
    public int insertVariety(Variety variety) {
        variety.setDelFlag("0");
        //检查名称唯一
        if(varietyDao.queryByVarietyName(variety.getVarietyName()) != null){
            return -1;
        }
        variety.setCreateTime(new Date());
        variety.setCreateBy(((User) session.getAttribute("userInfo")).getUserName());
        return varietyDao.insertVariety(variety);
    }
    /**
     * 根据实体修改商品品类
     * @param variety
     * @return
     */
    @Override
    public int updateVariety(Variety variety) {
        variety.setUpdateTime(new Date());
        variety.setUpdateBy(((User) session.getAttribute("userInfo")).getUserName());
        return varietyDao.updateVariety(variety);
    }
    /**
     * 删除商品品类
     * @param varietyId
     * @return
     */
    @Override
    public int deleteVariety(Long varietyId) {
        return varietyDao.deleteVariety(varietyId);
    }
    /**
     * 检查商品品类名称唯一
     * @param varietyId
     * @return
     */
    @Override
    public int checkVarietyUsed(Long varietyId) {
        return varietyDao.checkVarietyUsed(varietyId);
    }
    /**
     * 修改商品品类状态
     * @param varietyId
     * @param status
     * @return
     */
    @Override
    public int updateVarietyStatus(Long varietyId, String status) {
        return varietyDao.updateVarietyStatus(varietyId,status);
    }
    /**
     * 查询所有正常商品品类
     * @return
     */
    @Override
    public List<Variety> queryAllNormalVariety() {
        return varietyDao.queryAllNormalVariety();
    }
}
